package com.mef.smsplat.web.controller.sys.group;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hc24.util.IDUtil;
import com.mef.smsplat.dao.base.DataGridModel;
import com.mef.smsplat.dao.base.Page;
import com.mef.smsplat.domain.CompanyInfoDomain;
import com.mef.smsplat.domain.GroupInfoDomain;
import com.mef.smsplat.domain.PersonInfoDomain;
import com.mef.smsplat.domain.SysAdminInfoDomain;
import com.mef.smsplat.exception.BusinessException;
import com.mef.smsplat.service.GroupInfoService;
import com.mef.smsplat.util.Constants;
import com.mef.smsplat.web.controller.base.Json;

@Controller
@RequestMapping("/sys/group")
public class GroupInfoController {
	private GroupInfoService groupInfoService;

	public void setGroupInfoService(GroupInfoService groupInfoService) {
		this.groupInfoService = groupInfoService;
	}

	@ResponseBody
	@RequestMapping("/query.sys")
	public Page<GroupInfoDomain> query(String groupName,DataGridModel dgm) {
		return groupInfoService.query(groupName, dgm);
	}

	@ResponseBody
	@RequestMapping("/add.sys")
	public Json add(HttpSession session,String groupName,String remark,String[] personIds,String[] companyIds) {
		if (groupName == null || "".equals(groupName.trim())) {
			throw new BusinessException("群组姓名不能为空");
		}
		if (!groupInfoService.isUnique(groupName, "-1")) {
			throw new BusinessException("此群组名已经存在");
		}

		//获取session
		SysAdminInfoDomain sysAdmin = (SysAdminInfoDomain) session.getAttribute(Constants.SESSION_SYS_ADMIN);
		GroupInfoDomain group = new GroupInfoDomain();
		group.setGroupId(IDUtil.next() + "");
		group.setGroupName(groupName);
		group.setRemark(remark);
		if(sysAdmin!=null){
			group.setCreatePerson(sysAdmin.getSysAdminId());
			group.setCreateName(sysAdmin.getAdminName());
			group.setCreateType(Constants.SESSION_SYS_ADMIN);
		}
		group.setCreateTime(new Date());
		return new Json();
	}
	
	@ResponseBody
	@RequestMapping("/addMember.sys")
	public Json addMember(String groupId,String[] personIds,String[] companyIds) {
		GroupInfoDomain group = groupInfoService.getById(groupId);
		groupInfoService.addGroup(group,personIds,companyIds,group.getGroupId());
		return new Json();
	}

	@ResponseBody
	@RequestMapping("/update.sys")
	public Json update(GroupInfoDomain group) {
		if (group.getGroupName() == null || "".equals(group.getGroupName().trim())) {
			throw new BusinessException("群组姓名不能为空");
		}
		if (!groupInfoService.isUnique(group.getGroupName(), group.getGroupId())) {
			throw new BusinessException("此群组名已经存在");
		}
		groupInfoService.update(group);
		return new Json();
	}

	@ResponseBody
	@RequestMapping("/del.sys")
	public Json del(String[] ids) {
		groupInfoService.removeById(ids);
		//删除关联组表中的信息
		groupInfoService.deleteRelateGroup(ids);
		return new Json();
	}
	
	@ResponseBody
	@RequestMapping("/queryAll.sys")
	public List<GroupInfoDomain> queryAll(){
		return groupInfoService.queryAll();
	}
	
	@ResponseBody
	@RequestMapping("/queryPerson.sys")
	public Page<PersonInfoDomain> queryPerson(HttpServletRequest request,String groupId,DataGridModel dgm) {
		return groupInfoService.queryPerson(groupId,dgm);
	}
	
	@ResponseBody
	@RequestMapping("/queryCompany.sys")
	public Page<CompanyInfoDomain> queryCompany(HttpServletRequest request,String groupId,DataGridModel dgm) {
		return groupInfoService.queryCompany(groupId,dgm);
	}

}
